﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class ConsultingAdd : PowerBase
    {
        CJJCH.BLL.T_NET_Consulting bll_con = new CJJCH.BLL.T_NET_Consulting();
        CJJCH.BLL.T_NET_MenuSecond bll_menuSecond = new CJJCH.BLL.T_NET_MenuSecond();
        CJJCH.BLL.T_NET_MenuThird bll_menuThird = new CJJCH.BLL.T_NET_MenuThird();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.form1.DefaultButton = "btnSave";
            if (!IsPostBack)
            {
                InitControl();
            }
            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }
        private void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"].Trim()))
            {
                CJJCH.Model.T_NET_MenuSecond model = new CJJCH.Model.T_NET_MenuSecond();
                model = bll_menuSecond.GetMenuSecondModel(Convert.ToInt32(Request.QueryString["secondid"].ToString()));
                if (model != null)
                {
                    this.lblSecondID.Text = model.ID.ToString();
                    this.ConsultingTop1.SecondId = model.ID;
                    this.lblSecondName.Text = model.MenuName;
                    this.ConsultingTop1.SecondName = model.MenuName;
                    this.lblFirstID.Text = model.FirstID.ToString();
                    if (!model.HaveChild)
                    {
                        this.tdType.Style.Add("display", "none;");
                        this.ConsultingTop1.TypeID = 0;
                    }
                    else
                    {
                        this.ConsultingTop1.TypeID = 1;
                        IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
                        list = bll_menuThird.GetDrop(Convert.ToInt32(this.lblSecondID.Text.Trim()));
                        int typeCount = list.Count;
                        if (typeCount > 0)
                        {
                            for (int i = 0; i < typeCount; i++)
                            {
                                dropThirdID.Items.Add(new ListItem(list[i].MenuName, list[i].ID.ToString()));
                            }
                            list = null;
                        }
                    }
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ConsultingList.aspx?secondid="
                    + LTP.Common.StringClass.EncodeUrl(lblSecondID.Text.Trim()));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string error = checkInput();
            if (string.IsNullOrEmpty(error))
            {
                CJJCH.Model.T_NET_Consulting model_con = new CJJCH.Model.T_NET_Consulting();
                model_con.ID = Guid.NewGuid();
                model_con.DataAuthor = LTP.Common.StringClass.EncodeString(this.txtAuthor.Text.Trim());
                model_con.DataNote = LTP.Common.StringClass.RemoveSQL(this.txtNote.Value.Trim());
                model_con.DataSource = LTP.Common.StringClass.EncodeString(this.txtSource.Text.Trim());
                model_con.DataTitle = LTP.Common.StringClass.EncodeString(this.txtTitle.Text.Trim());
                model_con.FirstID = Convert.ToInt32(this.lblFirstID.Text.Trim());
                model_con.Hits = 0;
                model_con.ManagerID = SessionManagerID;
                    model_con.OrderNum = Convert.ToInt32(this.txtOrderNum.Text.Trim());
                
                    model_con.PlusDate = Convert.ToDateTime(this.txtPlusDate.Text.Trim());
                
                    model_con.SecondID = Convert.ToInt32(this.lblSecondID.Text.Trim());
	            
                if (tdType.Style["display"] != "none")
                {
                    if (!string.IsNullOrEmpty(this.dropThirdID.SelectedValue) && LTP.Common.CheckInput.CheckDigit(this.dropThirdID.SelectedValue))
                    {
                        model_con.ThirdID = Convert.ToInt32(this.dropThirdID.SelectedValue);
                    }
                    else
                    {
                        model_con.ThirdID = 0;
                    }
                }
                else
                {
                    model_con.ThirdID = 0;
                }
                string res= bll_con.AddConsulting(model_con);
                if ( res== "")
                {
                    if (chkAgain.Checked)
                    {
                        LTP.Common.MessageBox.ShowAndRedirect(this, "添加成功！", "ConsultingAdd.aspx?secondid="
                            + LTP.Common.StringClass.EncodeUrl(this.lblSecondID.Text.Trim()));
                    }
                    else
                    {
                        LTP.Common.MessageBox.ShowAndRedirect(this, "添加成功！", "ConsultingList.aspx?secondid="
                            + LTP.Common.StringClass.EncodeUrl(this.lblSecondID.Text.Trim()));
                    }
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(res);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }

        protected string checkInput()
        {
            StringBuilder error = new StringBuilder();
            if (string.IsNullOrEmpty(this.txtTitle.Text.Trim()))
            {
                error.Append("请输入编号！ \\n");
            }
            if (string.IsNullOrEmpty(this.txtSource.Text.Trim()))
            {
                error.Append("请输入项目名称！ \\n");
            }
            if (string.IsNullOrEmpty(this.txtAuthor.Text.Trim()))
            {
                error.Append("请输入委托单位！ \\n");
            }
            if (string.IsNullOrEmpty(this.txtPlusDate.Text.Trim()))
            {
                error.Append("请选择评估日期！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDateTime(this.txtPlusDate.Text.Trim()))
            {
                error.Append("选择评估日期有误！ \\n");
            }
            if (tdType.Style["display"] != "none")
            {
                if (string.IsNullOrEmpty(this.dropThirdID.SelectedValue))
                {
                    error.Append("请选择所属类别！ \\n");
                }
                else if (!LTP.Common.CheckInput.CheckDigit(this.dropThirdID.SelectedValue))
                {
                    error.Append("请选择正确的类别！ \\n");
                }
            }
            if (string.IsNullOrEmpty(this.txtNote.Value.Trim()))
            {
                error.Append("请输入详情内容！ \\n");
            }
            if (String.IsNullOrEmpty(this.txtOrderNum.Text.Trim()))
            {
                error.Append("请输入显示排序号！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.txtOrderNum.Text.Trim()))
            {
                error.Append("显示排序请输入整数！ \\n");
            }
            return error.ToString();
        }
    }
}